package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.service.AdminUserService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminUserServiceImpl implements AdminUserService {
    @Override
    public List<MarketUser> list(String page, String limit, String sort, String order, String username, String mobile) {
        SqlSession session = MybatisUtil.getSession();
        MarketUserMapper userMapper = session.getMapper(MarketUserMapper.class);
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if(!StringUtils.isEmpty(username)){
            criteria.andUsernameLike("%" + username + "%");
        }
        if(!StringUtils.isEmpty(mobile)){
            criteria.andMobileLike("%" + mobile + "%");
        }
        marketUserExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);
        session.commit();
        session.close();
        return marketUsers;
    }

    @Override
    public void update(String nickname,String status,String gender,String password,String mobile,String id) {

        SqlSession session = MybatisUtil.getSession();
        MarketUserMapper mapper = session.getMapper(MarketUserMapper.class);
        MarketUser marketUser = mapper.selectByPrimaryKey(Integer.parseInt(id));
        marketUser.setNickname(nickname);
        marketUser.setUsername(nickname);
        marketUser.setStatus(Byte.parseByte(status));
        marketUser.setGender(Byte.parseByte(gender));
        marketUser.setPassword(password);
        marketUser.setMobile(mobile);
        mapper.updateByPrimaryKey(marketUser);
        session.commit();
        session.close();
    }
}
